<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="openDrawer('add')"
          >
            添加面经
          </el-button>
        </div>
      </template>

      <el-table :data="list" stripe style="width: 100%">
        <el-table-column prop="stem" label="标题" width="400"></el-table-column>
        <el-table-column prop="creator" label="作者"></el-table-column>
        <el-table-column prop="likeCount" label="点赞"></el-table-column>
        <el-table-column prop="views" label="浏览数"> </el-table-column>
        <el-table-column prop="createdAt" label="更新时间"> </el-table-column>
        <el-table-column label="操作" width="100">
          <template v-slot="{ row }">
            <div class="handle">
              <i
                class="el-icon-view"
                @click="openDrawer('preview', row.id)"
              ></i>
              <i
                class="el-icon-edit-outline"
                @click="openDrawer('edit', row.id)"
              ></i>
              <i class="el-icon-delete" @click="delArticle(row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="pageSize"
        @current-change="pullData"
      ></el-pagination>
    </el-card>
    <!-- 编辑抽屉 -->
    <el-drawer
      :title="drawerTitle"
      :visible.sync="drawer"
      direction="rtl"
      size="50%"
      @closed="closeDrawer"
    >
      <el-card class="box-card" v-if="drawerType === 'preview'">
        <div slot="header" class="clearfix">
          <h4>{{ form.stem }}</h4>
        </div>
        <div class="text item" v-html="form.content"></div>
      </el-card>
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        :rules="rules"
        v-else
      >
        <el-form-item label="标题" prop="stem">
          <el-input
            v-model.trim="form.stem"
            placeholder="输入面经标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quillEditor
            @blur="$refs.form.validateField('content')"
            v-model.trim="form.content"
          ></quillEditor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="pushArticel">完成</el-button>
          <el-button @click="drawer = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import {
  addArticle,
  delArticle,
  getArticle,
  getArticleDetail,
  upArticle
} from '@/api/article'
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'article-page',
  data () {
    return {
      form: {
        stem: '',
        content: ''
      },
      list: [],
      current: 1,
      pageSize: 10,
      total: 0,
      drawer: false, // 编辑抽屉开关
      drawerType: '',
      rules: {
        stem: [{ required: true, message: '请输入标题名称', trigger: 'blur' }],
        content: [
          { required: true, message: '请输入面经内容', trigger: 'blur' }
        ]
      },
      articleId: ''
    }
  },
  created () {
    this.upDataArticle()
  },
  methods: {
    closeDrawer () {
      try {
        this.$refs.form.resetFields()
      } catch (e) {}
    },
    async upDataArticle () {
      const { data } = await getArticle({
        current: this.current,
        pageSize: this.pageSize
      })
      this.list = data.rows
      this.total = data.total
    },
    pullData (current) {
      this.current = current
      this.upDataArticle()
    },
    async openDrawer (type, id) {
      this.drawerType = type
      this.articleId = id
      this.drawer = true
      const { data } = await getArticleDetail(id)
      this.form.stem = data.stem
      this.form.content = data.content
    },
    async pushArticel () {
      if (this.drawerType === 'add') {
        // 添加提交
        this.$refs.form.validate(async (bl) => {
          if (!bl) return
          await addArticle({
            stem: this.form.stem,
            content: this.form.content
          })
          this.upDataArticle()
          this.$message.success('添加成功！')
          this.$refs.form.resetFields()
          this.drawer = false
        })
      } else if (this.drawerType === 'edit') {
        // 编辑提交
        await upArticle({
          id: this.articleId,
          ...this.form
        })
        await this.upDataArticle()
        this.drawer = false
        this.$message.success('编辑完成！')
      }
    },
    async delArticle (id) {
      await delArticle(id)
      await this.upDataArticle()
      this.$message.success('删除成功！')
    }
  },
  computed: {
    drawerTitle () {
      return {
        add: '添加面经',
        preview: '预览',
        edit: '编辑面经'
      }[this.drawerType]
    }
  },
  components: {
    quillEditor
  }
}
</script>

<style lang="scss" scoped>
.handle {
  display: flex;
  justify-content: space-between;
  font-size: 18px;
}
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
